/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

#include "common/lang/comparator.h"
#include "common/lang/sstream.h"
#include "common/log/log.h"
#include "common/type/null_type.h"
#include "common/value.h"

RC NULLType::set_value_from_str(Value &val, const string &data) const
{
  RC rc = RC::SUCCESS;
  if(data!="NULL")
  {
    rc = RC::SCHEMA_FIELD_TYPE_MISMATCH;
    return rc;
  }
  val.set_null();
  return rc;
}
RC NULLType::add(const Value &left, const Value &right, Value &result) const
{
  result.set_null();
  return RC::SUCCESS;
}

RC NULLType::subtract(const Value &left, const Value &right, Value &result) const
{
  result.set_null();
  return RC::SUCCESS;
}

RC NULLType::multiply(const Value &left, const Value &right, Value &result) const
{
  result.set_null();
  return RC::SUCCESS;
}

RC NULLType::negative(const Value &val, Value &result) const
{
  result.set_null();
  return RC::SUCCESS;
}

RC NULLType::to_string(const Value &val, string &result) const
{
  result = "NULL";
  return RC::SUCCESS;
}

RC NULLType::divide(const Value &left, const Value &right, Value &result) const
{
  result.set_null();
  return RC::SUCCESS;
}

int NULLType::compare(const Value &left, const Value &right) const
{
  // NULL值总是被认为是不等于任何值的，包括NULL本身
  // if (left.attr_type() == AttrType::NULLS && right.attr_type() == AttrType::NULLS) {
  //   return 0;
  // }
  //此处只在聚合函数哈希时用，因为NULL也会被聚合，因此将NULL视为相等
  if (left.attr_type() == AttrType::NULLS && right.attr_type() == AttrType::NULLS) {
    return 0;
  }
  if (left.attr_type() == AttrType::NULLS) {
    return -1; // NULL小于任何非NULL值
  }
  if (right.attr_type() == AttrType::NULLS) {
    return 1; // 任何非NULL值大于NULL
  }

  return 0;
}